﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace shopform
{
    public partial class LoginForm : Form
    {
        /// <summary>
        /// Actual tries of login.
        /// </summary>
        private int tryCount;

        /// <summary>
        /// Maximum tries of login.
        /// </summary>
        private static readonly int maxTryCount = 3;  
        
            /// <summary>
        /// Initializes LoginForm.
        /// </summary>
        public LoginForm()
        {
            InitializeComponent();
            this.tryCount = 0;
        }

        /// <summary>
        /// When Exit Button is Clicked we close the application.
        /// </summary>
        /// <param name="sender">Sender Object</param>
        /// <param name="e">EventArgs</param>
        private void ButtonExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// When Login Button is Clicked we try to authenticate the user.
        /// We count the number of tries, after 'maxTryCount' 
        /// incorrect logins we close the window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonLogin_Click(object sender, EventArgs e)
        {
            this.tryCount++;
            myAuthenticator.checkAuth(TBUserName.Text, TBPassword.Text);
            if (myAuthenticator.isAuth())
            {
                this.Close();
            }
            else
            {
                if (maxTryCount - tryCount == 0)
                {
                    throw new Exception("Tul sok sikertelen bejelentkezes!");
                }
                MessageBox.Show("Felhasznalo vagy jelszo helytelen!");
            }
        }
    }
}
